﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using RED_NET_Core.Entity;

namespace RED_NET_Core.Utilty
{
    public class ReturnValue{
       public int value;
       public string error;

    }
   public class Safety
   {
       private const int TitleLen = 30;
       private const int DailyLen =1000;
       private const int LetterLen = 500;
       private const int MessageLen = 60;
       private  static string delHtmlTag(string str)
       {
           return Regex.Replace(str, @"<[^> ]+>", ""); 
       }
       #region[判断日志标题]
       public static ReturnValue JudgeDailyTitle(string str)
       {
           ReturnValue ans = new ReturnValue();
           if (str.Length== 0)
           {
               ans.error = "标题不能为空";
           }
           else if (str.Length > 30)
           {
               ans.error = "标题过长";
           }
           else
           {
               var i = 0;
               for (i = 0; i < str.Length; i++)
               {
                   if (str[i] == '<' || str[i] == '>')
                       break;
               }
               if (i == str.Length)
               {
                   ans.value = 1;
               }
               else
               {
                   ans.error = "标题包含非法字符";
                   ans.value = 0;
               }
           }
           return ans;

       }
       #endregion
       #region[判断日志内容是否合法]
      public  static  ReturnValue  JudgeDailyContent(string str) {
          ReturnValue ans = new ReturnValue();
    if (str.Length > 5000) {
        ans.value = 0;
        ans.error = "内容过多";
        return ans;
    }
    else if (str.Length == 0)
    {
        ans.value = 0;
        ans.error = "内容不能为空";
        return ans;
    }
    else {
        ans.value = 1;
        return ans;
    }
    
}
       #endregion
      #region[判断评论是否合法]
      public static ReturnValue JudgeComment(string str)
      {
          ReturnValue ans = new ReturnValue();
          string tstr = delHtmlTag(str);
          if (str.Length > 1000)
          {
              ans.value = 0;
              ans.error = "评论内容过多";
              return ans;
          }
          else if (tstr.Length >1000)
          {
              ans.value = 0;
              ans.error = "评论过长";
              return ans;
          }
          else
          {
              ans.value = 1;
              return ans;
          }
      }
      #endregion
      #region[判断留言是否合法]
       public static ReturnValue JudgeMessage(string sstr) {
           ReturnValue ans = new ReturnValue();
    string  tstr = delHtmlTag(sstr);
    if (sstr.Length == 0) {
        ans.value = 0;
        ans.error = "留言不能为空";
        return ans;
    }
    else if (sstr.Length > 2000) {
        ans.value = 0;
        ans.error = "留言内容过多";
        return ans;
    }
    else if (tstr.Length > 1000) {
        ans.value = 0;
        ans.error = "留言过长";
        return ans;
    }
    else {
        ans.value = 1;
        return ans;
    }
    
}
      #endregion
       #region[判断站内信标题是否合法]
    public static  ReturnValue  JudgeLetterTitle(string tstr) {
          ReturnValue  ans = new ReturnValue();
    ans.value = 0;
    if (tstr.Length == 0) {
        ans.error = "标题不能为空";
    }
    else if (tstr.Length > 30) {
        ans.error = "标题过长";
    }
    else {
        int i = 0;
        for (i = 0; i < tstr.Length; i++) {
            if (tstr[i] == '<' || tstr[i] == '>')
                break;
        }
        if (i == tstr.Length) {
            ans.value = 1;
        }
        else {
            ans.error = "标题包含非法字符";
            ans.value = 0;
        }
    }
    return ans;

}
       #endregion
      #region[判断站内信内容是否合法]
     public static  ReturnValue  JudgeLetterContent(string tstr) {
           ReturnValue ans = new ReturnValue();
    if (tstr.Length > 5000) {
        ans.value = 0;
        ans.error = "内容过多";
        return ans;
    }
    else if (tstr.Length == 0) {
        ans.value = 0;
        ans.error = "内容不能为空";
        return ans;
    }
    else {
        ans.value = 1;
        return ans;
    }

}
      #endregion
     #region[判断英雄帖标题]
       public static ReturnValue  JudgeHeroTitle(string tstr) {
           ReturnValue ans = new ReturnValue();
    ans.value = 0;
    if (tstr.Length == 0) {
        ans.error = "标题不能为空";
    }
    else if (tstr.Length > 30) {
        ans.error = "标题过长";
    }
    else {
        var i = 0;
        for (i = 0; i < tstr.Length; i++) {
            if (tstr[i] == '<' || tstr[i] == '>')
                break;
        }
        if (i == tstr.Length) {
            ans.value = 1;
        }
        else {
            ans.error = "标题包含非法字符";
            ans.value = 0;
        }
    }
    return ans;

}
     #endregion
       #region[判断英雄帖内容]
       public static ReturnValue JudgeHeroContent(string tstr) {
           ReturnValue ans = new ReturnValue();
    if (tstr.Length > 500) {
        ans.value = 0;
        ans.error = "内容过多";
        return ans;
    }
    else if (tstr.Length == 0) {
        ans.value = 0;
        ans.error = "内容不能为空";
        return ans;
    }
    else {
        ans.value = 1;
        return ans;
    }

}
       #endregion
       #region[判断输入的文本是否合法]
       /// <summary>
       /// 判断输入的文本是否合法
       /// </summary>
       ///  /// <param name="type">1:表示判断的是标题，2：日志3：站内信4：留言 5:评论 6:英雄帖</param>
       /// <param name="str"></param>
       /// <returns>返回0表示合法,1：字数过长,2:文本为空,3:标题包涵非法输入，</returns>
       public static ReturnValue JudgeStr(string str, int type)
       {
           ReturnValue ret=new ReturnValue();
           if (str.Length > 1100)
           {
               ret.error = "内容过多";
               return ret;
           }
           string tstr = "";
           int j;
           for (j = 0; j < str.Length; j++)
           {
               if (str[j] == '<')
               {
                   if (j + 1 < str.Length && str[j + 1] == 'i' && (type == 4 || type == 5))
                   {
                       ret.error = "内容非法";
                       ret.value = 3;
                       return ret;
                   }
                   while (str[j] != '>'&&j<str.Length)
                       j++;
               }
               else
                   tstr += str[j];
           }
           str = tstr;
           int i, len=0;
           str = str.Trim();
           if (type == 1)
               len = TitleLen;
           else if (type == 2)
           {
               len = DailyLen;
           }
           else if (type == 3)
           {
               len = LetterLen;
           }
           else if (type == 4||type==5||type==6)
           {
               len = MessageLen;
           }

            
           if (str.Length >len)
           {
               ret.value = 1;
               if (type == 1)
                   ret.error = "标题过长";
               else if (type == 2)
               {
                   ret.error = "日志内容过多";
               }
               else if (type == 3)
               {
                   ret.error = "站内信字数过多";
               }
               else if (type == 4)
               {
                   ret.error = "留言过长";
               }
               else if (type == 5)
               {
                   ret.error = "评论过长";
               }
               if (type == 6)
               {
                   ret.error = "内容过长";
               }
           }
           else if (str.Length == 0)
           {
               ret.value = 2;
               if (type == 1)
                   ret.error = "标题不能为空";
               else if (type == 2)
               {
                   ret.error = "日志内容不能为空";
               }
               else if (type == 3)
               {
                   ret.error = "站内信内容不能为空";
               }
               else if (type == 4)
               {
                   ret.error = "留言不能为空";
               }
               else if (type == 5)
               {
                   ret.error = "评论不能为空";
               }
               else if (type == 6)
               {
                   ret.error = "内容不能为空";
               }

           }
           else
           {
               if (type == 1)
               {
                 
                   for (i = 0; i < str.Length; i++)
                   {
                       if (str[i] == '<' || str[i] == '>')
                       {
                           ret.value = 3;
                           ret.error = "非法输入";
                       }
                   }
               }
           }
           return ret;
       }
       #endregion
       #region[判断密码是否合法]
       /// <summary>
       /// 
       /// </summary>
       /// <param name="str"></param>
       /// <param name="lenmin"></param>
       ///  <param name="lenmax"></param>
       /// <returns>返回0表示合法,4:密码长度至少为lenmin个字节，5：密码包含非法字符，6：长度过长，7：密码为空 </returns>
       public static ReturnValue JudgePass(string str,int lenmin,int lenmax)
       { 
           
           ReturnValue rt = new ReturnValue();
           if (str.Length == 0)
           {
               rt.value = 7;
               rt.error = "密码不能为空！";
               return rt;
           }
           else if(str.Length<lenmin)
           {
               rt.value = 4;
               rt.error = "密码至少为"+lenmin.ToString()+"个字符";
               return rt;
           }           
           else if(str.Length>lenmax)
           {
               rt.value=6;
               rt.error="密码长度最多为"+lenmax.ToString()+"个字符";
               return rt;
           }
          int i;
           for (i = 0; i < str.Length; i++)
           {
               if ((str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= 'a' && str[i] <= 'z') || (str[i] >= '0' && str[i] <= '9') || (str[i] == '_'))
                   continue;
               else
                  
               {
                   rt.value=5;
                   rt.error="密码只能包含字母、数字或下划线！";
                   return rt;
               }
           }
           rt.value=0;
           return rt;
       
           
       }
       #endregion
       public static bool   IsChineseLetter(string input,int index)
  {        int code = 0;
        int chfrom = Convert.ToInt32("4e00", 16);    //范围（0x4e00～0x9fff）转换成int（chfrom～chend）
        int chend = Convert.ToInt32("9fff", 16);
        if (input != "")
      {
             code = Char.ConvertToUtf32(input, index);    //获得字符串input中指定索引index处字符unicode编码
            
           if (code >= chfrom && code <= chend)     
            {
                 return true;     //当code在中文范围内返回true

             }
            else
           {
                  return false ;    //当code不在中文范围内返回false
             }
         }          return false;
 }


       #region[判断字符串的合法性]
       public static ReturnValue IsValidStr(string str, int maxlen)
       {
           ReturnValue rt = new ReturnValue();
           if (str.Length == 0)
           {
               rt.error = "不能为空";
               rt.value = 8;
           }
           else if (str.Length > maxlen)
           {
               rt.error = "长度过长！";
               rt.value = 9;
           }
           else
           {
               int i;
               for (i = 0; i < str.Length; i++)
               {
                   if (IsChineseLetter(str, i))
                       continue;
                   else if ((str[i] >= '0' && str[i] <= '9') || (str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= 'a' && str[i] <= 'z'))
                       continue;
                   else
                   {
                       rt.error = "包含非法字符";
                       rt.value = 10;
                       return rt;
                   }
               }
           }
           return rt;
       }
       #endregion
     
      public static string getShortStr(string str,int num)
       {
           string ans = "";
           int i;
           for (i = 0; i < str.Length; i++)
           {
               if (str[i] == '<')
               {
                   while (str[i] != '>')
                       i++;
               }
               else
                   ans += str[i];

           }
           if (ans.Length <= num)
               return ans;
           else
               return ans.Substring(0, num);
       }



   }
}
